import os
import sys
import time

import pymysql


def save_stat_to_db(_filename):
    print(_filename)
    rows = []
    with open(_filename, encoding='UTF-8') as file_obj:
        data = file_obj.readlines()
        print(data)
        addnum = 0
        subnum = 0
        author = ''
        for line in data:
            aa = line.find('\t')
            if aa > -1:
                value1 = line[0:aa]
                addnum += 0 if value1 == '-' else int(value1)
                substr = line[aa + 1:len(line)]
                value2 = substr[0:substr.find('\t')]
                subnum += 0 if value2 == '-' else int(value2)
            bb = line.find('author:')
            if bb > -1:
                name = line[7:len(line) - 1]
                if author == '':
                    author = name
                elif author != name:
                    add_row(addnum, author, rows, subnum)
                    addnum = 0
                    subnum = 0
                    author = name
        if author != '':
            add_row(addnum, name, rows, subnum)

    print(rows)


def add_row(addnum, name, rows, subnum):
    check = True
    for row in rows:
        if row['name'] == name:
            newrow = {'name': name, 'addnum': row['addnum'] + addnum, 'subnum': row['subnum'] + subnum}
            rows.remove(row)
            rows.append(newrow)
            check = False
            break
    if check:
        rows.append({'name': name, 'addnum': addnum, 'subnum': subnum})


if __name__ == '__main__':
    save_stat_to_db('D:/gitstat/saf-acm-iam-be-complete.txt')
